package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Customer;
import service.CustomerService;

import java.util.List;

@Controller
@RequestMapping("/hair")
public class CustomerController {
    @Autowired
    @Qualifier("CustomerImpl")
    CustomerService service;

    @RequestMapping("/allCustomer/{pageSize}")
    public String list(Model model, @PathVariable("pageSize") Integer pageSize){
        System.out.println(pageSize);
        List<Customer> list = service.selectCustomer(pageSize);
        int page = service.sumCustomer();
        model.addAttribute("list", list);
        model.addAttribute("page", page);
        return "allCustomer";
    }

    @RequestMapping("/123")
    public String List(Model model, String args){
        List<Customer> list = service.queryCustomer(args);
        model.addAttribute("list", list);
        return "allCustomer";
    }

    @RequestMapping("/toAddCustomer")
    public String toAddCustomer()
    {
        return "addCustomer";
    }

    @RequestMapping("/addCustomer")
    public String addCustomer(Customer customer)
    {
        System.out.println(customer);
        if(customer.getName() == null || customer.getIphone() == null)
        {
            System.out.println("NULL");
            return "hair/toAddCustomer";
        }else{
            service.addCustomer(customer);
            return "redirect:/hair/allCustomer/0";
        }
    }

    @RequestMapping("/toUpdateNum/{iphone}")
    public String toUpdateNum(Model model,@PathVariable("iphone") String iphone, Customer customer) {
        System.out.println(customer);
        Customer num = service.oneCustomer(iphone);
        System.out.println(num);
        model.addAttribute("num",num );
        service.updateCustomer(customer);
        Customer num1 = service.oneCustomer(iphone);
        System.out.println(num1);
        return "redirect:/hair/allCustomer/0";
    }


    @RequestMapping("/toUpdateCustomer")
    public String toUpdateCustomer(Model model,String iphone) {
        Customer customer = service.oneCustomer(iphone);
        System.out.println(customer);
        model.addAttribute("customer",customer );
        return "updateCustomer";
    }

    @RequestMapping("/updateCustomer")
    public String updateCourse(Model model, Customer customer) {
        System.out.println(customer);
        String iphone = customer.getIphone();
        service.updateCustomer(customer);
        Customer customers = service.oneCustomer(iphone);
        model.addAttribute("customers", customers);
        return "redirect:/hair/allCustomer/0";
    }

    @RequestMapping("/del/{id}")
    public String delCustomer(@PathVariable("id") int id) {
        service.delCustomer(id);
        return "redirect:/hair/allCustomer/0";
    }

}
